<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Biquyet extends CI_Controller {
	public function index(){
		if($this->session->userdata('user_id') == '' || $this->session->userdata('user_name') == ''){
			$this->load->helper(array('url'));
			redirect('/useraz/login', 'location', 301);
		}
		$this->layout->setTitle('B&#237; Quy&#7871;t | Qu&#7843;n l&#253;');
		$page = $this->uri->segment(3) > 1 ? $this->uri->segment(3) : 1;
		if ($page > 1) $page = $page / PAGE_ITEM + 1;
		$offset = ($page - 1) * PAGE_ITEM;
		if(isset($_GET['s'])){
			$strFind = quotes_to_entities($_GET['s']);
			$filter = " AND tieude LIKE '%". $strFind ."%' OR cuphap LIKE '%". $strFind ."%'";
			$data['find'] = $strFind;
		}
		$sqlCustomer = "SELECT SQL_CALC_FOUND_ROWS * FROM tbl_biquyet WHERE 1=1 $filter ORDER BY biquyet_id DESC LIMIT $offset, ".PAGE_ITEM;
		$queryCustomer = $this->db->query($sqlCustomer);
		// get total
		$sqlTotal = "SELECT FOUND_ROWS() AS found_row";
		$queryTotal = $this->db->query($sqlTotal);
		$dataTotal = $queryTotal->row();
		if($dataTotal->found_row > 0){
			if($dataTotal->found_row > PAGE_ITEM){
				$this->load->library('pagination');
				$config['base_url'] = $this->config->item('base_url').'/biquyet/index/';
				$config['total_rows'] = $dataTotal->found_row;
				$config['per_page'] = PAGE_ITEM;
				$config['num_tag_open'] = '<li>';
				$config['num_tag_close'] = '</li>';
				$config['cur_tag_open'] = '<li><span>';
				$config['cur_tag_close'] = '</span></li>';
				$config['prev_link'] = 'Previous';
				$config['prev_tag_open'] = '<li>';
				$config['prev_tag_close'] = '</li>';
				$config['next_link'] = 'Next';
				$config['next_tag_open'] = '<li>';
				$config['next_tag_close'] = '</li>';
				$config['uri_segment'] = 3;
				$config['first_tag_open'] = '<li>';
				$config['first_tag_close'] = '</li>';
				$config['last_tag_open'] = '<li>';
				$config['last_tag_close'] = '</li>';
				
				$this->pagination->initialize($config);
				$data['paginate'] = $this->pagination->create_links();
			}
			$data['total'] = $dataTotal->found_row;
			if ($queryCustomer->num_rows() > 0){
				foreach ($queryCustomer->result_array() as $row){
					$data['items'][] = $row;
				}
			}
		}
		$this->layout->view(THEME_DIR.'/biquyet_index', $data);
	}
	public function add(){
		$this->layout->setTitle('B&#237; quy&#7871;t | Th&#234;m m&#7899;i');
		$this->load->helper(array('string_func', 'string'));
		if(!empty($_POST)){
			if($_POST['tieude'] == ''){
				$errMsg[] = 'C&#7847;n nh&#7853;p ti&#234;u &#273;&#7873;';
			}			
			if($_POST['noidung'] == ''){
				$errMsg[] = 'N&#7897;i dung tin nh&#7855;n';
			}
			if($_POST['link'] == ''){
				$errMsg[] = '&#272;&#432;&#7901;ng d&#7851;n tr&#7843; v&#7873;';
			}
			if ($_POST['cuphap'] == ''){
				$errMsg[] = 'C&#250; ph&#225;p tin nh&#7855;n';
			}
			if(empty($errMsg)){
				// check for existing customer
				$sqlCheck = "SELECT biquyet_id FROM tbl_biquyet WHERE tieude='".quotes_to_entities($_POST['tieude'])."' OR cuphap='".quotes_to_entities($_POST['cuphap'])."'";
				$queryCheck = $this->db->query($sqlCheck);				
				
				if($queryCheck->num_rows() > 0){
					$errMsg[] = "&#272;&#227; t&#7891;n t&#7841;i ti&#234;u &#273;&#7873; hay c&#250; ph&#225;p tin nh&#7855;n.";
				}				
				else{
					$arrData = array(
						'tieude'		=> $_POST['tieude'],
						'noidung'	=> $_POST['noidung'],
						'link'	=> $_POST['link'],
						'cuphap'	=> $_POST['cuphap'],						
					);
					if($this->db->insert('tbl_biquyet', $arrData)){
						$data['msg'] = "&#272;&#227; th&#234;m m&#7899;i th&#224;nh c&#244;ng tin nh&#7855;n b&#237; quy&#7871;t m&#249;a thi.";
					}
					else{
						$errMsg[] = "C&#243; l&#7895;i x&#7843;y ra. Vui l&#242;ng th&#7917; l&#7841;i";
					}
				}
			}
			
			if(!empty($errMsg)){
				$data['error'] = true;
				$data['error_msg'] = $errMsg;
			}
		}
		$this->layout->view(THEME_DIR.'/biquyet_add', $data);
	}
	public function edit(){
		$this->layout->setTitle('B&#237; quy&#7871;t | Ch&#7881;nh s&#7917;a');
		$this->load->helper(array('string_func', 'string'));
		$bid = intval($this->uri->segment(3));
		$sqlcustomer = "SELECT * FROM tbl_biquyet WHERE biquyet_id='$bid'";
		$rscustomer = $this->db->query($sqlcustomer);
		if($rscustomer->num_rows() > 0){
			$data['biquyetInfo'] = $rscustomer->row_array();
			if(!empty($_POST)){				
					$arrData = array(
						'tieude'		=> $_POST['tieude'],
						'noidung'	=> $_POST['noidung'],
						'link'	=> $_POST['link'],
						'cuphap'	=> $_POST['cuphap'],
					);
					
					if($this->db->update('tbl_biquyet', $arrData, array("biquyet_id" => $bid))){
						$data['msg'] = "C&#7853;p nh&#7853;t th&#224;nh c&#244;ng.";
						$arrData['tieude'] = $data['biquyetInfo']['tieude'];
						$data['biquyetInfo'] = $arrData;
					}
					else{
						$errMsg[] = "C&#243; l&#7895;i x&#7843;y ra";
					}
				}
				
				if(!empty($errMsg)){
					$data['error'] = true;
					$data['error_msg'] = $errMsg;
				}
		}
		else{
			$data['error'] = true;
			$data['error_msg'] = array('Kh&#244;ng t&#236;m th&#7845;y');
		}
		$this->layout->view(THEME_DIR.'/biquyet_edit', $data);
	}	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */